package com.fr.view;

import com.fr.base.BaseUtils;
import com.fr.base.FCloneable;
import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.dav.Env;
import com.fr.base.print.PrintUtils;
import com.fr.cell.core.layout.LayoutFactory;
import com.fr.dialog.core.ParameterInputDialog;
import com.fr.report.FineBook;
import com.fr.report.PageSet;
import com.fr.report.ReportPage;
import com.fr.report.ResultWorkBook;
import com.fr.report.TemplateWorkBook;
import com.fr.report.core.ReportHelper;
import com.fr.report.core.ReportUtils;
import com.fr.report.parameter.Parameter;
import com.fr.view.core.InnerPreviewPane;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.print.PrinterException;
import java.util.Collections;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

public class PreviewPane extends JPanel
{
  private JButton firstButton;
  private JButton nextButton;
  private JTextField pageText;
  private JButton previousButton;
  private JButton lastButton;
  private JButton printButton;
  private InnerPreviewPane innerPreviewPane;
  private int currentPage;
  private TemplateWorkBook backupTemplate;
  private TemplateWorkBook currentTemplate;
  private PageSet reportPageSet = null;
  private int currentIndex = -1;
  private Map parameterMap = Collections.EMPTY_MAP;
  private ActionListener nextActionListener = new ActionListener(this)
  {
    private final PreviewPane this$0;

    public void actionPerformed()
    {
      PreviewPane.access$300(this.this$0);
    }
  };
  private ActionListener previousActionListener = new ActionListener(this)
  {
    private final PreviewPane this$0;

    public void actionPerformed()
    {
      PreviewPane.access$400(this.this$0);
    }
  };
  private ActionListener firstActionListener = new ActionListener(this)
  {
    private final PreviewPane this$0;

    public void actionPerformed()
    {
      PreviewPane.access$500(this.this$0);
    }
  };
  private ActionListener lastActionListener = new ActionListener(this)
  {
    private final PreviewPane this$0;

    public void actionPerformed()
    {
      PreviewPane.access$600(this.this$0);
    }
  };
  private ActionListener printActionListener = new ActionListener(this)
  {
    private final PreviewPane this$0;

    public void actionPerformed()
    {
      try
      {
        PrintUtils.print(PreviewPane.access$700(this.this$0).getTraversingCase());
      }
      catch (PrinterException localPrinterException)
      {
        FRContext.getLogger().log(Level.WARNING, localPrinterException.getMessage(), localPrinterException);
        JOptionPane.showMessageDialog(this.this$0, localPrinterException.getMessage());
      }
    }
  };
  private MouseListener firstMouseListener = new MouseListener(this)
  {
    private final PreviewPane this$0;

    public void mouseClicked()
    {
    }

    public void mouseEntered()
    {
      if (PreviewPane.access$800(this.this$0).isEnabled())
        PreviewPane.access$800(this.this$0).setBorderPainted(true);
    }

    public void mouseExited()
    {
      PreviewPane.access$800(this.this$0).setBorderPainted(false);
    }

    public void mousePressed()
    {
    }

    public void mouseReleased()
    {
    }
  };
  private MouseListener previousMouseListener = new MouseListener(this)
  {
    private final PreviewPane this$0;

    public void mouseClicked()
    {
    }

    public void mouseEntered()
    {
      if (PreviewPane.access$900(this.this$0).isEnabled())
        PreviewPane.access$900(this.this$0).setBorderPainted(true);
    }

    public void mouseExited()
    {
      PreviewPane.access$900(this.this$0).setBorderPainted(false);
    }

    public void mousePressed()
    {
    }

    public void mouseReleased()
    {
    }
  };
  private MouseListener nextMouseListener = new MouseListener(this)
  {
    private final PreviewPane this$0;

    public void mouseClicked()
    {
    }

    public void mouseEntered()
    {
      if (PreviewPane.access$1000(this.this$0).isEnabled())
        PreviewPane.access$1000(this.this$0).setBorderPainted(true);
    }

    public void mouseExited()
    {
      PreviewPane.access$1000(this.this$0).setBorderPainted(false);
    }

    public void mousePressed()
    {
    }

    public void mouseReleased()
    {
    }
  };
  private MouseListener lastMouseListener = new MouseListener(this)
  {
    private final PreviewPane this$0;

    public void mouseClicked()
    {
    }

    public void mouseEntered()
    {
      if (PreviewPane.access$1100(this.this$0).isEnabled())
        PreviewPane.access$1100(this.this$0).setBorderPainted(true);
    }

    public void mouseExited()
    {
      PreviewPane.access$1100(this.this$0).setBorderPainted(false);
    }

    public void mousePressed()
    {
    }

    public void mouseReleased()
    {
    }
  };
  private MouseListener printMouseListener = new MouseListener(this)
  {
    private final PreviewPane this$0;

    public void mouseClicked()
    {
    }

    public void mouseEntered()
    {
      if (PreviewPane.access$1200(this.this$0).isEnabled())
        PreviewPane.access$1200(this.this$0).setBorderPainted(true);
    }

    public void mouseExited()
    {
      PreviewPane.access$1200(this.this$0).setBorderPainted(false);
    }

    public void mousePressed()
    {
    }

    public void mouseReleased()
    {
    }
  };

  public PreviewPane()
  {
    setLayout(LayoutFactory.createBorderLayout());
    JToolBar localJToolBar = new JToolBar();
    add(localJToolBar, "North");
    localJToolBar.setLayout(new FlowLayout(0, 4, 2));
    this.firstButton = new JButton();
    this.firstButton.setIcon(BaseUtils.readIcon("/com/fr/cell/images/tabscroll/first.png"));
    this.firstButton.setToolTipText(Inter.getLocText("First"));
    this.firstButton.setMnemonic('F');
    this.firstButton.setBorderPainted(false);
    this.firstButton.setOpaque(false);
    this.firstButton.addMouseListener(this.firstMouseListener);
    this.firstButton.addActionListener(this.firstActionListener);
    localJToolBar.add(this.firstButton);
    this.previousButton = new JButton();
    this.previousButton.setIcon(BaseUtils.readIcon("/com/fr/cell/images/tabscroll/left.png"));
    this.previousButton.setToolTipText(Inter.getLocText("ReportServerP-Previous"));
    this.previousButton.setMnemonic('P');
    this.previousButton.setBorderPainted(false);
    this.previousButton.setOpaque(false);
    this.previousButton.addMouseListener(this.previousMouseListener);
    this.previousButton.addActionListener(this.previousActionListener);
    localJToolBar.add(this.previousButton);
    this.pageText = new JTextField(3);
    this.pageText.setHorizontalAlignment(0);
    this.pageText.addActionListener(new ActionListener(this)
    {
      private final PreviewPane this$0;

      public void actionPerformed()
      {
        PreviewPane.access$100(this.this$0, Integer.parseInt(PreviewPane.access$000(this.this$0).getText().toString()) - 1);
        PreviewPane.access$000(this.this$0).setText((PreviewPane.access$200(this.this$0) + 1) + "");
      }
    });
    localJToolBar.add(this.pageText);
    this.nextButton = new JButton();
    this.nextButton.setIcon(BaseUtils.readIcon("/com/fr/cell/images/tabscroll/right.png"));
    this.nextButton.setToolTipText(Inter.getLocText("ReportServerP-Next"));
    this.nextButton.setMnemonic('N');
    this.nextButton.setBorderPainted(false);
    this.nextButton.setOpaque(false);
    this.nextButton.addMouseListener(this.nextMouseListener);
    this.nextButton.addActionListener(this.nextActionListener);
    localJToolBar.add(this.nextButton);
    this.lastButton = new JButton();
    this.lastButton.setSize(1, 1);
    this.lastButton.setIcon(BaseUtils.readIcon("/com/fr/cell/images/tabscroll/last.png"));
    this.lastButton.setToolTipText(Inter.getLocText("ReportServerP-Last"));
    this.lastButton.setMnemonic('L');
    this.lastButton.setBorderPainted(false);
    this.lastButton.setOpaque(false);
    this.lastButton.addMouseListener(this.lastMouseListener);
    this.lastButton.addActionListener(this.lastActionListener);
    localJToolBar.add(this.lastButton);
    JSeparator localJSeparator = new JSeparator();
    localJToolBar.add(localJSeparator);
    this.printButton = new JButton();
    this.printButton.setIcon(BaseUtils.readIcon("/com/fr/design/images/m_file/print.png"));
    this.printButton.setToolTipText(Inter.getLocText("Print"));
    localJToolBar.add(this.printButton);
    this.printButton.setMnemonic('T');
    this.printButton.setBorderPainted(false);
    this.printButton.setOpaque(false);
    this.printButton.addMouseListener(this.printMouseListener);
    this.printButton.addActionListener(this.printActionListener);
    this.innerPreviewPane = new InnerPreviewPane();
    add(new JScrollPane(this.innerPreviewPane), "Center");
  }

  private void checkButtonEnabled()
  {
    this.firstButton.setEnabled(false);
    this.previousButton.setEnabled(false);
    this.nextButton.setEnabled(false);
    this.lastButton.setEnabled(false);
    this.printButton.setEnabled(false);
    if (this.currentIndex > 0)
    {
      this.previousButton.setEnabled(true);
      this.firstButton.setEnabled(true);
    }
    if ((!(this.reportPageSet.sizePredictable())) || (this.currentIndex < this.reportPageSet.size() - 1))
    {
      this.nextButton.setEnabled(true);
      this.lastButton.setEnabled(true);
    }
    if (this.reportPageSet.size() > 0)
      this.printButton.setEnabled(true);
  }

  private void showPage(int paramInt)
  {
    if (paramInt < 0)
      paramInt = 0;
    if (paramInt == this.currentIndex)
      return;
    ReportPage localReportPage = this.reportPageSet.getPage(paramInt);
    if ((localReportPage == null) && (this.reportPageSet.sizePredictable()))
    {
      paramInt = this.reportPageSet.size() - 1;
      localReportPage = this.reportPageSet.getPage(paramInt);
    }
    this.innerPreviewPane.print(localReportPage);
    this.currentIndex = paramInt;
    checkButtonEnabled();
  }

  private void showFirstPage()
  {
    showPage(0);
    this.currentPage = 0;
    this.pageText.setText((this.currentPage + 1) + "");
  }

  private void showPreviousPage()
  {
    showPage(Math.max(0, this.currentIndex - 1));
    this.currentPage = Math.max(0, this.currentIndex - 1);
    this.pageText.setText((this.currentIndex + 1) + "");
  }

  private void showNextPage()
  {
    showPage(this.currentIndex + 1);
    this.currentPage = this.currentIndex;
    this.pageText.setText((this.currentPage + 1) + "");
  }

  private void showLastPage()
  {
    showPage(2147483647);
    this.currentPage = ((this.reportPageSet.sizePredictable()) ? this.currentIndex + 1 : this.reportPageSet.size());
    this.pageText.setText(this.currentPage + "");
  }

  public void print(TemplateWorkBook paramTemplateWorkBook)
  {
    this.backupTemplate = paramTemplateWorkBook;
    Env localEnv = FRContext.getCurrentEnv();
    try
    {
      this.currentTemplate = ((TemplateWorkBook)paramTemplateWorkBook.clone());
      Parameter[] arrayOfParameter1 = localEnv.getTemplateParameters(this.currentTemplate);
      Parameter[] arrayOfParameter2 = new Parameter[arrayOfParameter1.length];
      for (int i = 0; i < arrayOfParameter1.length; ++i)
        arrayOfParameter2[i] = ((Parameter)arrayOfParameter1[i].clone());
      if ((arrayOfParameter2 != null) && (arrayOfParameter2.length > 0))
      {
        localObject = ParameterInputDialog.showWindow(SwingUtilities.getWindowAncestor(this), arrayOfParameter2);
        if (((ParameterInputDialog)localObject).getReturnValue() != 0)
          throw new RuntimeException("cancel");
        this.parameterMap = ((ParameterInputDialog)localObject).update();
      }
      ReportHelper.clearFormulaResult(this.currentTemplate);
      Object localObject = localEnv.executeTemplate(this.currentTemplate, this.parameterMap);
      print(((ResultWorkBook)localObject).generateReportPageSet(ReportUtils.getPaperSettingListFromWorkBook((FineBook)localObject)));
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.SEVERE, localException.getMessage(), localException);
      throw new RuntimeException(localException.getMessage(), localException);
    }
  }

  public void print(PageSet paramPageSet)
  {
    this.reportPageSet = paramPageSet;
    this.currentIndex = -1;
    this.innerPreviewPane.print((ReportPage)null);
    checkButtonEnabled();
    showNextPage();
  }

  public void releasePageSet()
  {
    if (this.reportPageSet != null)
      this.reportPageSet.release();
  }

  static JTextField access$000(PreviewPane paramPreviewPane)
  {
    return paramPreviewPane.pageText;
  }

  static void access$100(PreviewPane paramPreviewPane, int paramInt)
  {
    paramPreviewPane.showPage(paramInt);
  }

  static int access$200(PreviewPane paramPreviewPane)
  {
    return paramPreviewPane.currentIndex;
  }

  static void access$300(PreviewPane paramPreviewPane)
  {
    paramPreviewPane.showNextPage();
  }

  static void access$400(PreviewPane paramPreviewPane)
  {
    paramPreviewPane.showPreviousPage();
  }

  static void access$500(PreviewPane paramPreviewPane)
  {
    paramPreviewPane.showFirstPage();
  }

  static void access$600(PreviewPane paramPreviewPane)
  {
    paramPreviewPane.showLastPage();
  }

  static PageSet access$700(PreviewPane paramPreviewPane)
  {
    return paramPreviewPane.reportPageSet;
  }

  static JButton access$800(PreviewPane paramPreviewPane)
  {
    return paramPreviewPane.firstButton;
  }

  static JButton access$900(PreviewPane paramPreviewPane)
  {
    return paramPreviewPane.previousButton;
  }

  static JButton access$1000(PreviewPane paramPreviewPane)
  {
    return paramPreviewPane.nextButton;
  }

  static JButton access$1100(PreviewPane paramPreviewPane)
  {
    return paramPreviewPane.lastButton;
  }

  static JButton access$1200(PreviewPane paramPreviewPane)
  {
    return paramPreviewPane.printButton;
  }
}